package com.example.electronicmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.electronicmall.dao.ItemDao;
import com.example.electronicmall.entity.ItemShopEntity;
import com.example.electronicmall.service.ItemService;
import com.example.electronicmall.service.ItemShopService;
import com.example.electronicmall.to.ItemTo;
import com.example.electronicmall.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.electronicmall.entity.ItemEntity;


@Service("itemService")
public class ItemServiceImpl extends ServiceImpl<ItemDao, ItemEntity> implements ItemService {

    @Autowired
    ItemShopService itemShopService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        return null;
    }

    @Override
    public void saveItem(ItemTo itemTo) {
        ItemEntity item = new ItemEntity();
        item.setName(itemTo.getName());
        item.setInfo(itemTo.getInfo());
        item.setPrice(itemTo.getPrice());
        Date date = new Date();
        item.setCreatedAt(date);
        item.setUpdatedAt(date);
        this.baseMapper.insert(item);

        Integer itemId = this.baseMapper.selectOne(new QueryWrapper<ItemEntity>().eq("name", itemTo.getName())).getItemId();
        ItemShopEntity is = new ItemShopEntity();
        is.setItemId(itemId);
        is.setShopId(itemTo.getShopId());
        is.setOnSale(itemTo.getOnSale());
        itemShopService.getBaseMapper().insert(is);
    }

    @Override
    public List<ItemEntity> listItemsByShopId(Integer shopId) {
        List<ItemShopEntity> isList = itemShopService.getBaseMapper().selectList(new QueryWrapper<ItemShopEntity>().eq("shop_id", shopId).eq("on_sale", 1));
        for (ItemShopEntity s : isList) {
            System.out.println(s);
        }
        return isList.stream().map((v) -> this.baseMapper.selectOne(new QueryWrapper<ItemEntity>().eq("item_id", v.getItemId()))).collect(Collectors.toList());
    }

    @Override
    public List<ItemTo> listAllItemsByShopId(Integer shopId) {
        List<ItemShopEntity> isList = itemShopService.getBaseMapper().selectList(new QueryWrapper<ItemShopEntity>().eq("shop_id", shopId));
        return isList.stream().map(v -> {
            ItemEntity item = this.baseMapper.selectOne(new QueryWrapper<ItemEntity>().eq("item_id", v.getItemId()));
            ItemTo itemTo = new ItemTo();
            itemTo.setItemId(v.getItemId());
            itemTo.setName(item.getName());
            itemTo.setPrice(item.getPrice());
            itemTo.setInfo(item.getInfo());
            itemTo.setOnSale(v.getOnSale());
            return itemTo;
        }).collect(Collectors.toList());
    }

    @Override
    public void upItem(Integer itemId) {
        ItemShopEntity is = itemShopService.getBaseMapper().selectOne(new QueryWrapper<ItemShopEntity>().eq("item_id", itemId));
        is.setOnSale(1);
        itemShopService.updateById(is);
    }

    @Override
    public void downItem(int itemId) {
        ItemShopEntity is = itemShopService.getBaseMapper().selectOne(new QueryWrapper<ItemShopEntity>().eq("item_id", itemId));
        is.setOnSale(0);
        itemShopService.updateById(is);
    }
}